# -*- coding:utf-8 -*-

from typing import Mapping, Any

from src._typing import OCREngines, MatchLocation
from src._base import logger

__all__ = ('TextMatch',)


class TextMatch:

    from src._items._paddleocr import PaddleOCRReader
    from src._items._easyocr import EasyOCRReader

    # 默认支持的OCR识别引擎
    _engine_type: OCREngines | str = ''
    # OCR识别引擎
    _engine = None
    # OCR识别引擎字典
    _engines: Mapping[str, Any] = {
        'paddleocr': PaddleOCRReader,
        'easyocr': EasyOCRReader,
    }

    def __init__(self, engine: str | None = 'paddleocr', image_path: str | None = None,
                 model_path: str | None = None, language: str | None = None):
        # 更新默认OCR识别引擎类型
        if engine is not None and engine not in self._engines:
            logger.warning(f'当前不支持的引擎: {engine}， 将默认使用: paddleocr 引擎!')
            engine = 'paddleocr'
        self._engine_type = engine
        logger.info(f'当前使用的OCR识别引擎: {self._engine_type}')
        # 初始化OCR识别引擎
        self._engine = self._engines[engine](
            image_path=image_path,
            model_base_path=model_path,
            language=language,
        )

    @property
    def engine_type(self) -> str:
        return self._engine_type

    def update_special_symbol(self, special_symbol: str):
        """
        更新特殊符号
        Args:
            special_symbol(str): 特殊符号信息

        Returns:

        """
        self._engine.regex_special(special_symbol)

    def get_ocr_original_result(self) -> list:
        """
        获取OCR识别引擎原始结果
        Returns:

        """
        return self._engine._get_result()

    def text_get_match_location(self, text: str, is_filter_special_symbol: bool = True) -> MatchLocation | None:
        """
        获取文本匹配位置
        Args:
            text(str): 待匹配文本
            is_filter_special_symbol(bool): 是否过滤特殊符号

        Returns:
            MatchLocation | None: 匹配位置,没有匹配到结果时为None

        """
        return self._engine.text_match_location(
            text=text,
            is_filter_special_symbol=is_filter_special_symbol
        )

    def texts_get_match_location(self, texts: list[str] | set[str] | tuple[str],
                                 is_filter_special_symbol: bool = True
                                 ) -> list[MatchLocation] | list:
        """
        获取文本匹配位置
        Args:
            texts(list[str] | set[str] | tuple[str]): 待匹配文本列表
            is_filter_special_symbol(bool): 是否过滤特殊符号

        Returns:
            list[MatchLocation] | list: 匹配位置列表,都没又匹配到时为空list

        """
        return self._engine.texts_match_location(
            texts=texts,
            is_filter_special_symbol=is_filter_special_symbol
        )

    def text_is_exists(self, text: str, is_filter_special_symbol: bool = True) -> bool:
        """
        判断文本是否存在
        Args:
            text(str): 待匹配文本
            is_filter_special_symbol(bool): 是否过滤特殊符号

        Returns:
            bool: 是否存在

        """
        return self._engine.text_is_match(
            text=text,
            is_filter_special_symbol=is_filter_special_symbol
        )

    def texts_is_all_exists(self, texts: list[str] | set[str] | tuple[str],
                            is_filter_special_symbol: bool = True) -> bool:
        """
        判断文本列表中所有文本是否存在
        Args:
            texts(list[str] | set[str] | tuple[str]): 待匹配文本列表
            is_filter_special_symbol(bool): 是否过滤特殊符号

        Returns:

        """
        return self._engine.texts_is_all_match(
            texts=texts,
            is_filter_special_symbol=is_filter_special_symbol
        )


if __name__ == '__main__':
    # eng = 'testocr'
    # test_engines: Mapping[str, Any] = {'paddleocr': 'abc', 'easyocr': 'def', }
    # if eng is not None and eng not in test_engines:
    #     print(f'当前不支持的引擎: {eng}， 将默认使用: paddleocr 引擎!')
    t_match = TextMatch(image_path='../test_pictures/numbers.jpeg')
